<template>
  <div>
    <navigation-bar>
      <div class="searchContent">
        <el-input v-model="planName" placeholder="请输入配置名称" size="mini"
                  style="width: 150px; margin-right: 10px"></el-input>
        <custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
        <custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
        <el-button
            type="success"
            size="mini"
            style="margin-right: 10px"
            :disabled="pldShow"
            @click="pleditClick"
        >
          <i class="el-icon-edit"></i> 批量编辑
        </el-button
        >
        <custom-button type="add" @addClick="addClick"></custom-button>

      </div>
    </navigation-bar>
    <table-index :dataSource="dataSource" @handleSelectionChange="handleSelectionChange" :selection="selection"
                 :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
                 :pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @editRow="editRow" @viewRow="viewRow"
                 @deleteRow="deleteRow"></table-index>

    <div v-if="dialogShow">
      <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
        <form-index :ReceiveData="ReceiveData" :nidShow="nidShow" :formConfig="formConfig" @submit="submit"
                    @formSelectChange="formSelectChange" ref="form"
                    :type="type">
          <!--  <div>这里还可以插入其它内容进行拓展</div> -->
        </form-index>
      </custom-dialog>
    </div>
    <el-dialog :title="tltleName" :visible.sync="dialogFormVisible" width="80%">
      <el-form :model="form" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="入组序号" :disabled="showsj" :label-width="formLabelWidth" v-if="tltleName != '批量编辑试验随机表'"
                          >
              <el-input :disabled="showsj" v-model="form.groupSeq" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="随机号" :label-width="formLabelWidth" v-if="tltleName != '批量编辑试验随机表'" >
              <el-input :disabled="showsj" v-model="form.randomNo" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划给药日期" :label-width="formLabelWidth" v-if="tltleName != '新增试验随机表'"
                          >
              <el-date-picker
                  :disabled="tltleName == '查看试验随机表'"
                  style="width:100%"
                  v-model="form.planDrugDate"
                  align="right"
                  type="date"
                  placeholder="选择日期"
                  :picker-options="pickerOptions"
                  @change="
								(e) => {
									dateChangeEnd(e)
								}
							"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分组名称" :label-width="formLabelWidth" v-if="tltleName != '新增试验随机表'" >
              <el-select v-model="form.groupName" placeholder="请选择活动区域" style="width:100%"
                         :disabled="tltleName == '查看试验随机表'">
                <el-option :label="select.label"
                           :value="select.value"
                           v-for="(select, sIndex) in groupList"
                           :key="sIndex"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="getsubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {TestTandomTableFormConfig} from "@/assets/form/managementCenter/testConfiguration/testTandomTableForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import {AjaxCommon} from "@/libs/apiCommon"
import {PopupCommon} from "@/libs/popupCommon"
// import { SessionCommon } from "@/libs/sessionCommon"
import moment from "moment"

export default {
  components: {
    navigationBar,
    customButton,
    tableIndex,
    customDialog,
    formIndex,
  },
  data() {
    return {
      showsj: false,
      tltleName: '',
      ReceiveData: [],//多选编辑数据
      pldShow: true,
      dialogFormVisible: false,
      form: {
        groupSeq: '',
        randomNo: '',
        groupName: '',
        planDrugDate: '',
      },
      formLabelWidth: '120px',
      groupList: [],
      planName: "",
      signPrefix: "",
      // table 相关参数  begin
      dataSource: [],
      selection: true,
      tableColumns: [
        {
          prop: "groupSeq",
          label: "入组顺序号",
        },
        {
          prop: "randomNo",
          label: "随机号",
        },
        {
          prop: "groupName",
          label: "分组名称",
        },
      ],
      btnList: ["edit", "view", "delete"],
      pageSize: 20,
      pageCount: 100,
      pageNo: 1,
      // table 相关参数  end

      // 弹窗是否显示
      dialogShow: false,
      // 弹窗标题 (新增修改不同)
      dialogTitle: "",

      // form的配置信息
      formConfig: null,
      type: "add",
      pickerOptions: {
        // disabledDate(time) {
        // 	return time.getTime() > Date.now();
        // },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
    }
  },

  methods: {
    dateChangeEnd(value) {
      this.form.planDrugDate = moment(value).format("YYYY-MM-DD HH:mm:ss")
      // console.log();
    },
    // 清空数据
    qkdata() {
      this.showsj = false,
          this.form = {
            groupSeq: '',
            randomNo: '',
            groupName: '',
            planDrugDate: '',
          }
    },
    // 接收多选编辑数据
    handleSelectionChange(val) {
      this.ReceiveData = val
      console.log(this.ReceiveData, 'this.ReceiveData');
      if (this.ReceiveData.length > 0) {
        this.pldShow = false
      } else if (this.ReceiveData.length == 0) {
        this.pldShow = true
      }
    },
    // 批量编辑
    pleditClick() {
      this.dialogFormVisible = true
      this.tltleName = '批量编辑试验随机表'
      this.qkdata()
    },
    formSelectChange(e) {
      if (e.filedName == 'groupId') {
        let item = this.groupList.find((c) => c.value == e.value)
        const formValue = this.$refs.form.getFormValue()
        formValue.groupName = item.label
        this.$refs.form.changeFormValue(formValue)
      }
    },
    getsubmit() {
      let self = this
      if (this.tltleName == '编辑试验随机表') {
        if (this.form.planDrugDate === "" || this.form.planDrugDate === null){
          PopupCommon.notify("info", "请选择计划给药日期")
          return;
        }
        if (this.form.groupName === "" || this.form.groupName === null){
          PopupCommon.notify("info", "请选择分组名称")
          return;
        }
        console.log(this.groupList, 'this.groupList');
        for (let index = 0; index < this.groupList.length; index++) {
          if (this.groupList[index].id == this.form.groupName) {
            this.form.groupId = this.groupList[index].id
            this.form.groupName = this.groupList[index].label
          }
          console.log(this.form, 'this.form');
        }
        AjaxCommon("/ccdc/cfg/exp/root/expRandom/edit", "post", this.form)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                self.initPage()
                self.dialogFormVisible = false
                this.qkdata()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      } else if (this.tltleName == '新增试验随机表') {
        if (this.form.groupSeq === "" || this.form.groupSeq === null){
          PopupCommon.notify("info", "请输入入组序号")
          return;
        }
        if (this.form.randomNo === "" || this.form.randomNo === null){
          PopupCommon.notify("info", "请输入随机号")
          return;
        }
        AjaxCommon("/ccdc/cfg/exp/root/expRandom/add", "post", this.form)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                self.initPage()
                self.dialogFormVisible = false
                this.qkdata()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      } else if (this.tltleName == '批量编辑试验随机表') {
        this.form.expRandomList = []
        for (let index = 0; index < this.groupList.length; index++) {
          if (this.groupList[index].id == this.form.groupName) {
            this.form.groupId = this.groupList[index].id
            this.form.groupName = this.groupList[index].label
          }
          console.log(this.form, 'this.form');
        }
        for (let index = 0; index < this.ReceiveData.length; index++) {
          this.form.expRandomList.push({
            id: this.ReceiveData[index].id,
            groupName: this.form.groupName,
            groupId: this.form.groupId,
            planDrugDate: this.form.planDrugDate
          })
        }
        let form = this.form.expRandomList
        AjaxCommon("/ccdc/cfg/exp/root/expRandom/editBatchByIds", "post", form)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                self.initPage()
                self.dialogFormVisible = false
                this.qkdata()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      }
    },
    submit(d) {
      let self = this
      if (d.id) {
        AjaxCommon("/ccdc/cfg/exp/root/expRandom/edit", "post", d)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                self.initPage()
                self.dialogClose()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      } else {
        delete d.id
        AjaxCommon("/ccdc/cfg/exp/root/expRandom/add", "post", d)
            .then((res) => {
              if (res.data.code == 200) {
                PopupCommon.notify("success", "操作成功")
                self.initPage()
                self.dialogClose()
              } else {
                PopupCommon.notify("error", res.data.message)
              }
            })
            .catch((err) => {
              PopupCommon.notify("error", err.message)
            })
      }
    },
    dialogClose() {
      this.dialogShow = false
    },
    searchClick() {
      this.pageNo = 1
      this.initPage()
    },
    resetClick() {
      this.planName = ""
      this.signPrefix = ""
    },
    addClick() {
      this.dialogFormVisible = true
      this.tltleName = '新增试验随机表'
      this.qkdata()
    },
    // 分页点击事件
    pageChange(p) {
      this.pageNo = p
      this.initPage()
    },
    editRow(d) {

      this.dialogFormVisible = true
      this.tltleName = '编辑试验随机表'
      this.qkdata()
      this.showsj = true
      this.form = d.data
    },
    viewRow(d) {
      this.dialogFormVisible = true
      this.tltleName = '查看试验随机表'
      this.qkdata()
      this.showsj = true
      this.form = d.data
    },
    deleteRow(d) {
      const id = d.data.id
      let self = this
      AjaxCommon(
          "/ccdc/cfg/exp/root/expRandom/delete?id=" + id,
          "delete", {optReason: d.optReason}
      ).then((res) => {
        console.log(res, "res...............")
        if (res.data.code == 200) {
          PopupCommon.notify("success", "删除成功")
          self.initPage()
          self.dialogClose()
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
    // 初始化事件
    initPage() {
      let self = this
      AjaxCommon("/ccdc/cfg/exp/root/expRandom/listByPage", "post", {
        // planName: this.planName,
        // signPrefix: this.signPrefix,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.data.code == 200) {
          self.dataSource = res.data.result.records
          self.pageCount = res.data.result.total
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },// 初始化事件
    initSelect() {
      let _this = this;
      AjaxCommon("/ccdc/businessSelect/expGroupSelect", "post", {}).then((res) => {
        if (res.data.code == 200) {
          _this.groupList = res.data.result;
          _this.formConfig = TestTandomTableFormConfig(res.data.result)


        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
  },
  mounted() {
    this.initPage()
    this.initSelect()

  },
}
</script>

<style></style>
